<!-- 该画面是辅助信息维护画面：用于展示辅助信息并可以打开辅助信息Sheet画面，以及删除辅助信息 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="1100px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :before-close="cancel"
  >
    <!-- 项目计划信息 -->
    <projectplanbase-info :project-plan-base-information="projectPlanBaseInformation" />
    <!-- 项目计划信息 -->
    <br>
    <div class="DescriptionTitle">{{ $t('projectAuxiliaryMainTitle') }}</div>
    <el-table v-loading="loading" :data="projectPlanOtherList" border>
      <el-table-column label="类别" prop="section" width="190px" />
      <el-table-column label="类型" prop="section_item" width="190px" />
      <el-table-column label="数值" prop="value" show-tooltip-when-overflow />
    </el-table>

    <el-button v-if="createFlag" type="text" icon="el-icon-plus" @click="handleAdd">{{ $t('addBtn') }}</el-button>
    <el-button v-else type="text" icon="el-icon-edit" @click="handleUpdate()">{{ $t('modifyBtn') }}</el-button>
    <el-button :disabled="createFlag" type="text" icon="el-icon-delete" @click="handleDelete()">{{ $t('deleteBtn') }}</el-button>

    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">{{ $t('close') }}</el-button>
    </div>

    <projectauxiliary-sheet ref="projectauxiliarysheet" @refreshList="getProjectPlanOtherList" />
  </el-dialog>
</template>

<script>
import { getProjectPlanCommonListApi, deleteProjectPlanCommonApi } from '@/views/projectbase/common/projectplancommon.api'
import projectplanbaseInfo from '../../projectplanbase/components/projectplanbase-info.vue';
import ProjectauxiliarySheet from './projectauxiliary-sheet.vue';

export default {
  components: { projectplanbaseInfo, ProjectauxiliarySheet },
  data() {
    return {
      title: undefined,
      openSheet: false,
      createFlag: true,
      loading: false,
      projectPlanId: undefined,
      projectPlanBaseInformation: {},
      projectPlanOtherList: [],
      queryparams: {
        projectPlanFK: undefined,
        page: 1,
        page_size: 1000
      },
      resultsItems: [],
      formData: {}
    };
  },
  computed: {
  },
  methods: {
    async openSheetDialog(data) {
      this.openSheet = !this.openSheet;
      this.projectPlanBaseInformation = data;
      this.projectPlanId = data.id;
      this.getProjectPlanOtherList()
    },
    cancel() {
      this.openSheet = !this.openSheet;
    },
    // 获取项目计划基础信息表
    async getProjectPlanOtherList() {
      this.loading = true
      this.queryparams.projectPlanFK = this.projectPlanId
      var resp = await getProjectPlanCommonListApi({ ...this.queryparams })
      if (resp.success && resp.data.results.length > 0) {
        this.createFlag = false
        this.projectPlanOtherList = resp.data.results
      } else {
        this.createFlag = true
        this.projectPlanOtherList = []
      }
      this.loading = false
    },
    // 新增按钮
    handleAdd() {
      this.$refs.projectauxiliarysheet.openSheetDialog(this.projectPlanBaseInformation)
    },
    // 修改按钮
    handleUpdate() {
      // 如果是修改画面，则将传给子组件的参数，替换为当前项目计划ID下对应的辅助信息
      this.formData = this.projectPlanOtherList.reduce((acc, item) => {
        acc[item.section + item.section_item] = item.value;
        return acc;
      }, {});
      this.$refs.projectauxiliarysheet.openSheetDialog(this.projectPlanBaseInformation, this.formData)
    },
    // 删除按钮
    async handleDelete() {
      if (await this.ConfirmMessageBox(this.$t('projectPlanCustomerMaintainTitle'), this.$t('projectAuxiliaryDeleteConfirm'), 'warning') === 'cancel') {
        return
      }
      const resp = await deleteProjectPlanCommonApi({ projectPlanFK: this.projectPlanId });
      if (resp.success) {
        this.targaMessage(this.$t('deleteSuccess'));
        this.getProjectPlanOtherList();
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    }
  }
};
</script>

<style scoped>
</style>
